<?php
/*******************************************************************************
 * 一些链接
 *******************************************************************************/
if($__f_sysvars['url_rewrite'] == 'html' || $__f_sysvars['url_rewrite'] == 'rewrite'){
	$vars['linkmore'] = array(
		'aboutus'	=> PUB_DIR.'news/aboutus/',
		'sales'		=> PUB_DIR.'news/sales/',
		'trends'	=> PUB_DIR.'news/trends/',
		'brand'		=> PUB_DIR.'brand/',
		'salesrank'	=> PUB_DIR.'goods/search.do?orderby_sales=desc',
	);
}else{
	$vars['linkmore'] = array(
		'aboutus'	=> WEB_DIR.'index.php?mdl=news&mark=aboutus',
		'sales'		=> WEB_DIR.'index.php?mdl=news&mark=sales',
		'trends'	=> WEB_DIR.'index.php?mdl=news&mark=trends',
		'brand'		=> WEB_DIR.'index.php?mdl=brand',
		'salesrank'	=> WEB_DIR.'index.php?mdl=goods&orderby_sales=desc',
	);
}

$oNews = new News();
$oGoods = new Goods();

// 广告
$_f_advert = cache_read('adverts.php');
$_f_adpos = cache_read('adposs.php');
foreach($_f_advert['pos_ads_mark'] as $pos => $advert_list){
	foreach($advert_list as $adverid => $advert_info){
		$advert_info['pos'] = $_f_adpos['list_id'][$advert_info['posid']];
		if($__f_sysvars['url_rewrite'] == 'html' || $__f_sysvars['url_rewrite'] == 'rewrite'){
			$advert_info['url'] = PUB_DIR."ads/{$advert_info['id']}.html";
		}else{
			$advert_info['url'] = WEB_DIR."index.php?mdl=ads&do=view&id={$advert_info['id']}";
		}
		// 关联商品信息
		if(!empty($advert_info['related_goods'])){
			$ids = explode(',', trim($advert_info['related_goods'], ', '));
			$advert_info['related_goods_list'] = array();
			foreach($ids as $id){
				$goodsinfo = $oGoods->get_row($id, '`id`,`catid`,`name`,`price`,`cover`,`recommend`,`label`,`labeltype`,`cover`,`filename`,`addtime`');
				if($__f_sysvars['url_rewrite'] == 'html' || $__f_sysvars['url_rewrite'] == 'rewrite'){
					if(empty($goodsinfo['filename'])){
						$goodsinfo['url'] = PUB_DIR.'goods/'.time_to_path($goodsinfo['addtime']).'/'.$goodsinfo['id'].'.html';
					}else{
						$goodsinfo['url'] = PUB_DIR.'goods/'.$goodsinfo['filename'].'.html'.$goods_query;
					}
				}else{
					$goodsinfo['url'] = WEB_DIR."index.php?mdl=goods&do=view&id={$goodsinfo['id']}";
				}
				$advert_info['related_goods_list'][] = $goodsinfo;
			}
		}
		$advert_list[$adverid] = $advert_info;
	}
	//shuffle($advert_list);	// 随机排序
	$_f_advert['pos_ads_mark'][$pos] = $advert_list;
}

$vars['indexFirstMainAd']	= $_f_advert['pos_ads_mark']['indexFirstMainAd'];
$vars['indexSecondCoupon']	= array_shift($_f_advert['pos_ads_mark']['indexSecondCoupon']);
$vars['indexGroupBuy']		= array_shift($_f_advert['pos_ads_mark']['indexGroupBuy']);

// 快报，新闻动态
$newslist = $oNews->get_list('*', array('catid' => 5), array('rank' => 'DESC', 'id' => 'DESC'), 1, 8);
foreach($newslist['list'] as $key => $news){
	if(empty($news['url'])){
		if($__f_sysvars['url_rewrite'] == 'html' || $__f_sysvars['url_rewrite'] == 'rewrite'){
			if(empty($news['filename'])){
				$news['url'] = PUB_DIR.'news/'.time_to_path($news['addtime']).'/'.$news['id'].'.html';
			}else{
				$news['url'] = PUB_DIR.'news/'.$news['filename'].'.html';
			}
		}else{
			$news['url'] = WEB_DIR."index.php?mdl=news&do=view&id={$news['id']}";
		}
	}
	$newslist['list'][$key] = $news;
}
$vars['newslist'] = $newslist['list'];


$settings = array(
	/*******************************************************************************
	 * 热销、热评、新品、精品
	 *******************************************************************************/

	'hotsales' => array(	// 热销商品
		'select'	=> array('recommend' => 'hot'), 
		'orderby'	=> array('orderby_sales' => 'DESC', 'id' => 'DESC'),
	),
	'hotcomment' => array(	// 热评商品
		'select'	=> array('isdel' => 0), 
		'orderby'	=> array('orderby_comments' => 'DESC', 'orderby_score' => 'DESC', 'orderby_views' => 'DESC', 'orderby_id' => 'DESC'),
	),
	'new' => array(			// 新品上市
		'select'	=> array('recommend' => 'new'), 
		'orderby'	=> array('id' => 'DESC'),
	),
	'good' => array(		// 精品推荐
		'select'	=> array('recommend' => 'good'), 
		'orderby'	=> array('id' => 'DESC'),
	),

	/*******************************************************************************
	 * 热蔵、直降、折扣、绝版
	 *******************************************************************************/

	'favorite' => array(	// 热藏商品
		'select' 	=> array('recommend' => 'good'),
		'orderby'	=> array('orderby_favorites' => 'DESC', 'id' => 'DESC'),
	),
	'down' => array(		// 疯狂直降
		'select' 	=> array('labeltype' => '1'),
		'orderby'	=> array('orderby_sales' => 'DESC', 'id' => 'DESC'),
	),
	'discount' => array(	// 超值折扣
		'select' 	=> array('labeltype' => '2'),
		'orderby'	=> array('orderby_sales' => 'DESC', 'id' => 'DESC'),
	),
	'limited' => array(		// 限量绝版
		'select' 	=> array('labeltype' => '3'),
		'orderby'	=> array('orderby_sales' => 'DESC', 'id' => 'DESC'),
	),

	/*******************************************************************************
	 * 销售排行榜
	 *******************************************************************************/
	
	'salesrank' => array(	// 销售排行榜
		'select'	=> array('kept' => " AND `sales`>0"),
		'orderby'	=> array('orderby_sales' => 'DESC', 'orderby_views' => 'DESC', 'orderby_id' => 'DESC'),
		'pagesize'	=> 10,
	),
);

foreach($settings as $setname => $set){
	$pagesize = isset($set['pagesize']) ? $set['pagesize'] : 6;
	$goodslist = $oGoods->get_list('`id`,`name`,`price`,`sales`,`recommend`,`label`,`labeltype`,`cover`,`filename`,`addtime`', $set['select'], $set['orderby'], 1, $pagesize);
	foreach($goodslist['list'] as $key => $goods){
		if($__f_sysvars['url_rewrite'] == 'html' || $__f_sysvars['url_rewrite'] == 'rewrite'){
			if(empty($goods['filename'])){
				$goods['url'] = PUB_DIR.'goods/'.time_to_path($goods['addtime']).'/'.$goods['id'].'.html';
			}else{
				$goods['url'] = PUB_DIR.'goods/'.$goods['filename'].'.html';
			}
		}else{
			$goods['url'] = WEB_DIR."index.php?mdl=goods&do=view&id={$goods['id']}";
		}
		$goodslist['list'][$key] = $goods;
	}
	$vars["goodslist_{$setname}"] = $goodslist['list'];
}

/*******************************************************************************
 * 推荐品牌
 *******************************************************************************/

$oBrand = new Brand();
$brandlist = $oBrand->get_list('*', null, array('rank' => 'DESC', 'id' => 'DESC'), 1, 20);
foreach($brandlist['list'] as $key => $brand){
	if($__f_sysvars['url_rewrite'] == 'html' || $__f_sysvars['url_rewrite'] == 'rewrite'){
		if(empty($brand['mark'])){
			$brand['href'] = PUB_DIR.'brand/v/'.$brand['id'].'.html';
		}else{
			$brand['href'] = PUB_DIR.'brand/v/'.$brand['mark'].'.html';
		}
	}else{
		$brand['href'] = WEB_DIR."index.php?mdl=brand&do=view&id={$brand['id']}";
	}
	$brandlist['list'][$key] = $brand;
}
$vars['brandlist'] = $brandlist['list'];
?>